package com.heima.article.service.impl;


import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.IApArticleService;
import com.heima.common.constants.UserConstant;
import com.heima.file.service.FileStorageService;
import com.heima.model.acticle.dtos.ArticleDto;
import com.heima.model.acticle.dtos.ArticleHomeDto;
import com.heima.model.acticle.pojos.ApArticle;
import com.heima.model.acticle.pojos.ApArticleConfig;
import com.heima.model.acticle.pojos.ApArticleContent;
import com.heima.model.common.dtos.ResponseResult;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 文章信息表，存储已发布的文章 服务实现类
 * </p>
 *
 * @author chana
 * @since 2024-08-13
 */
@Service
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements IApArticleService {

    @Resource
    private ApArticleMapper apArticleMapper;

    @Resource
    private ApArticleConfigMapper apArticleConfigMapper;

    @Resource
    private ApArticleContentMapper apArticleContentMapper;

    @Resource
    private Configuration configuration;

    @Resource
    private FileStorageService fileStorageService;

    private final String fileName = "article.ftl";

    @Override
    public List<ApArticle> load(Integer type, ArticleHomeDto dto) {
        if (UserConstant.LOAD_TAG_ALL.equals(dto.getTag())){
            dto.setTag(null);
        }
        if (null == dto.getSize() || dto.getSize() > 1000 || dto.getSize() < 0){
            dto.setSize(UserConstant.LOAD_LIMIT_PAGESIZE);
        }
        List<ApArticle> list = apArticleMapper.load(type, dto);
        return list;
    }

    /**
     * 文章内部调用
     * @param dto
     * @return
     */
    @Override
    @Transactional
    public ResponseResult afterSave(ArticleDto dto) {
        //三表
        ApArticle apArticle = new ApArticle();
        BeanUtils.copyProperties(dto,apArticle);
        apArticleMapper.insert(apArticle);

        ApArticleConfig apArticleConfig = new ApArticleConfig();
        apArticleConfig.setArticleId(apArticle.getId());
        apArticleConfig.setIsComment(0);
        apArticleConfig.setIsDown(0);
        apArticleConfig.setIsDelete(0);
        apArticleConfig.setIsForward(0);
        apArticleConfigMapper.insert(apArticleConfig);

        ApArticleContent apArticleContent = new ApArticleContent();
        apArticleContent.setArticleId(apArticle.getId());
        apArticleContent.setContent(dto.getContent());
        apArticleContentMapper.insert(apArticleContent);

        return ResponseResult.okResult(apArticle.getId());
    }

    /**
     * 文章上架
     * @param id
     */
    @Override
    public void upArticle(long id) throws IOException, TemplateException {
        LambdaQueryWrapper<ApArticleContent> wrapper = Wrappers.<ApArticleContent>lambdaQuery()
                .eq(ApArticleContent::getArticleId, id);
        ApArticleContent apArticleContent = apArticleContentMapper.selectOne(wrapper);
        //
        Template template = configuration.getTemplate(fileName);
        //文章内容
        Map<String,Object> data = new HashMap<>();
        JSONArray jsonArray = JSONArray.parseArray(apArticleContent.getContent());
        data.put("content",jsonArray);
        //
        template.process(data,new FileWriter("D://article.html"));

        //2.
        String url = fileStorageService
                .uploadHtmlFile(null, "article.html", new FileInputStream("D://article.html"));

        //3.
        ApArticle apArticle = new ApArticle();
        apArticle.setId(1302864436297482242L);
        apArticle.setStaticUrl(url);
        apArticleMapper.updateById(apArticle);
    }
}
